//
//  UPIdentityInformationView.m
//  Uang Pasti
//
//  Created by 李永彬 on 2025/8/26.
//

#import "UPIdentityInformationView.h"

@interface UPIdentityInformationView()

@end

@implementation UPIdentityInformationView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInitSubviews];
    }
    return self;
}

- (void)commonInitSubviews {
    // 设置背景图片
    UIImage *bgImage = [UIImage imageNamed:@"calendar_bg"];
    CGFloat bgAspectRatio = bgImage.size.width / bgImage.size.height;
    CGFloat bgWidth = [UIScreen mainScreen].bounds.size.width - 2 * 16;
    CGFloat bgHeight = bgWidth / bgAspectRatio;
    
    [self.containerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.centerX.equalTo(self);
        make.height.mas_equalTo(bgHeight);
        make.width.mas_equalTo(bgWidth);
        make.bottom.equalTo(self);
    }];
    
    // 创建内容
    [self setupContentViews];
}

- (void)setupContentViews {
    
    [self.containerImageView addSubview:self.titleLabel];

    [self.containerImageView addSubview:self.nameTitleLabel];
    [self.containerImageView addSubview:self.nameTextField];

    [self.containerImageView addSubview:self.separator1];
    
    [self.containerImageView addSubview:self.idTitleLabel];
    [self.containerImageView addSubview:self.idTextField];
    [self.containerImageView addSubview:self.separator2];
    
    [self.containerImageView addSubview:self.birthdayLabel];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickBirthday)];
    self.birthdayTextLabel.userInteractionEnabled = YES;
    [self.birthdayTextLabel addGestureRecognizer:tap];
    [self.containerImageView addSubview:self.birthdayTextLabel];
    
    [self.containerImageView addSubview:self.separator3];
    
    [self.containerImageView addSubview:self.confirmButton];
    
    CGFloat labelHeight = 50.0;
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.containerImageView).offset(20);
        make.leading.equalTo(self.containerImageView).offset(30);
        make.trailing.equalTo(self.containerImageView).offset(-30);
        make.height.mas_equalTo(25);
    }];
    
    [self.nameTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(50);
        make.leading.equalTo(self.containerImageView).offset(30);
        make.trailing.equalTo(self.nameTextField.mas_leading).offset(-10);
        make.height.mas_equalTo(labelHeight);
    }];
    
    [self.nameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.nameTitleLabel);
        make.width.greaterThanOrEqualTo(@100);
        make.trailing.equalTo(self.containerImageView).offset(-30);
        make.height.mas_equalTo(labelHeight);
    }];
    
    [self.separator1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameTitleLabel.mas_bottom).offset(1);
        make.leading.equalTo(self.containerImageView).offset(30);
        make.trailing.equalTo(self.containerImageView).offset(-30);
        make.height.mas_equalTo(1);
    }];

    [self.idTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.separator1.mas_bottom).offset(1);
        make.leading.equalTo(self.containerImageView).offset(30);
        make.trailing.equalTo(self.idTextField.mas_leading).offset(-10);
        make.height.mas_equalTo(labelHeight);
    }];

    [self.idTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.idTitleLabel);
        make.width.greaterThanOrEqualTo(@100);
        make.trailing.equalTo(self.containerImageView).offset(-30);
        make.height.mas_equalTo(labelHeight);
    }];

    [self.separator2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.idTitleLabel.mas_bottom).offset(1);
        make.leading.equalTo(self.containerImageView).offset(30);
        make.trailing.equalTo(self.containerImageView).offset(-30);
        make.height.mas_equalTo(1);
    }];

    [self.birthdayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.separator2.mas_bottom).offset(1);
        make.leading.equalTo(self.containerImageView).offset(30);
        make.trailing.equalTo(self.birthdayTextLabel.mas_leading).offset(-10);
        make.height.mas_equalTo(labelHeight);
    }];
    
    [self.birthdayTextLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.birthdayLabel);
        make.width.greaterThanOrEqualTo(@100);
        make.trailing.equalTo(self.containerImageView).offset(-30);
        make.height.mas_equalTo(labelHeight);
    }];
    
    [self.separator3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.birthdayLabel.mas_bottom).offset(1);
        make.leading.equalTo(self.containerImageView).offset(30);
        make.trailing.equalTo(self.containerImageView).offset(-30);
        make.height.mas_equalTo(1);
    }];
    
    UIImage *buttonImage = [UIImage imageNamed:@"button_bg_style5"];
    CGFloat buttonHeight = 50.0;
    CGFloat buttonAspectRatio = buttonImage.size.width / buttonImage.size.height;
    CGFloat buttonWidth = buttonHeight * buttonAspectRatio;
    [self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self).offset(-bottomsafeArea());
        make.centerX.equalTo(self.containerImageView);
        make.height.mas_equalTo(buttonHeight);
        make.width.mas_equalTo(buttonWidth);
    }];
}

#pragma mark - Lazy Properties
- (UIImageView *)containerImageView {
    if (!_containerImageView) {
        _containerImageView = [[UIImageView alloc] init];
        _containerImageView.contentMode = UIViewContentModeScaleAspectFit;
        _containerImageView.image = [UIImage imageNamed:@"calendar_bg"];
        _containerImageView.userInteractionEnabled = YES;
        [self addSubview:_containerImageView];
    }
    return _containerImageView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.text = LocalizableForKey(@"Identity Information");
        _titleLabel.font = [UIFont systemFontOfSize:13 weight:400];
        _titleLabel.textColor = [UIColor colorWithHexString:@"#FFFFFF"];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

- (UILabel *)nameTitleLabel {
    if (!_nameTitleLabel) {
        _nameTitleLabel = [[UILabel alloc] init];
        _nameTitleLabel.text = LocalizableForKey(@"Aadhaar Name");
        _nameTitleLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightMedium];
        _nameTitleLabel.textColor = [UIColor colorWithHexString:@"#727272"];
        _nameTitleLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _nameTitleLabel;
}

- (UITextField *)nameTextField {
    if (!_nameTextField) {
        _nameTextField = [[UITextField alloc] init];
        _nameTextField.borderStyle = UITextBorderStyleNone;
        _nameTextField.font = [UIFont systemFontOfSize:12 weight:UIFontWeightBold];
        _nameTextField.textColor = [UIColor colorWithHexString:@"#000000"];
        _nameTextField.backgroundColor = [UIColor clearColor];
        _nameTextField.keyboardType = UIKeyboardTypeDefault;
    }
    return _nameTextField;
}

- (UILabel *)idTitleLabel {
    if (!_idTitleLabel) {
        _idTitleLabel = [[UILabel alloc] init];
        _idTitleLabel.text = LocalizableForKey(@"Aadhaar Number");
        _idTitleLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightMedium];
        _idTitleLabel.textColor = [UIColor colorWithHexString:@"#727272"];
        _idTitleLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _idTitleLabel;
}

- (UITextField *)idTextField {
    if (!_idTextField) {
        _idTextField = [[UITextField alloc] init];
        _idTextField.borderStyle = UITextBorderStyleNone;
        _idTextField.font = [UIFont systemFontOfSize:12 weight:UIFontWeightBold];
        _idTextField.textColor = [UIColor colorWithHexString:@"#000000"];
        _idTextField.backgroundColor = [UIColor clearColor];
        _idTextField.keyboardType = UIKeyboardTypeDefault;
    }
    return _idTextField;
}

- (UILabel *)birthdayLabel {
    if (!_birthdayLabel) {
        _birthdayLabel = [[UILabel alloc] init];
        _birthdayLabel.text = LocalizableForKey(@"Birthday");
        _birthdayLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightMedium];
        _birthdayLabel.textColor = [UIColor colorWithHexString:@"#727272"];
        _birthdayLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _birthdayLabel;
}

- (UILabel *)birthdayTextLabel {
    if (!_birthdayTextLabel) {
        _birthdayTextLabel = [[UILabel alloc] init];
        _birthdayTextLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightBold];
        _birthdayTextLabel.textColor = [UIColor colorWithHexString:@"#000000"];
        _birthdayTextLabel.backgroundColor = [UIColor clearColor];
        _birthdayTextLabel.textAlignment = NSTextAlignmentRight;
    }
    return _birthdayTextLabel;
}

- (UIView *)separator1 {
    if (!_separator1) {
        _separator1 = [[UIView alloc] init];
        _separator1.backgroundColor = [UIColor colorWithHexString:@"#000000"];
    }
    return _separator1;
}

- (UIView *)separator2 {
    if (!_separator2) {
        _separator2 = [[UIView alloc] init];
        _separator2.backgroundColor = [UIColor colorWithHexString:@"#000000"];
    }
    return _separator2;
}

- (UIView *)separator3 {
    if (!_separator3) {
        _separator3 = [[UIView alloc] init];
        _separator3.backgroundColor = [UIColor colorWithHexString:@"#000000"];
    }
    return _separator3;
}

- (UIButton *)confirmButton {
    if (!_confirmButton) {
        _confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_confirmButton setBackgroundImage:[UIImage imageNamed:@"button_bg_style5"] forState: UIControlStateNormal];
        [_confirmButton setTitle:LocalizableForKey(@"Confirm") forState:UIControlStateNormal];
        [_confirmButton setTitleColor:[UIColor colorWithHexString:@"#000000"] forState:UIControlStateNormal];
        [_confirmButton addTarget:self action:@selector(clickConfirmBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _confirmButton;
}

- (void)clickConfirmBtnAction {
    if (self.confirmBlock) {
        self.confirmBlock(self.nameTextField.text, self.idTextField.text, self.birthdayTextLabel.text);
    }
}

- (void)setModel:(AuthLapidant *)model {
    _model = model;
    self.nameTextField.text = model.quindecimlocalety;
    self.idTextField.text = model.assumeization;
    id birthdayValue = model.movieion;
    NSString *birthdayText = (birthdayValue != [NSNull null]) ? birthdayValue : nil;
    if (birthdayText && birthdayText.length > 0 && ![birthdayText isEqualToString:@"//"]) {
        self.birthdayTextLabel.text = birthdayText;
    } else {
        self.birthdayTextLabel.text = nil;
    }
}

- (void)clickBirthday {
    if (self.birthdayBlock) {
        self.birthdayBlock(self.birthdayTextLabel.text);
    }
}

@end
